<!--
 * @Author: 王灿
 * @Date: 2019-11-28 08:37:58
 * @LastEditTime: 2020-03-19 20:03:03
 * @Description: 订单发货
 * @FilePath: \scarlet-sparrow-admin\src\views\order\order-deliver\index.vue
 -->
<template>
  <div class="order-deliver">
    <el-form :model="ruleForm" ref="ruleForm" :rules="rule">
      <!-- 快递名称 -->
      <el-form-item prop="expressName">
        <el-row>
        <el-select
          v-model="ruleForm.expressName"
          filterable
          remote
          reserve-keyword
          clearable
          placeholder="请选择快递"
          :remote-method="onGetExpress"
        >
          <el-option
            v-for="item in expressList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        </el-row>
      </el-form-item>
      <!-- 物流编号 -->
      <el-form-item prop="expressNum">
        <el-row>
          <el-input v-model="ruleForm.expressNum" placeholder="物流编号" clearable></el-input>
        </el-row>
      </el-form-item>
      <!-- 物流费用 -->
      <el-form-item prop="expressPrice">
        <el-row>
          <el-input v-model="ruleForm.expressPrice" placeholder="物流费用" clearable></el-input>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmitForm('ruleForm')">确 定</el-button>
      <el-button @click="onCancelForm('ruleForm')">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { getOrderDeliver } from '@/api/order.js'
import { getByType } from '@/api/configure.js'
export default {
  name: 'OrderDeliver',
  props: {
    // 默认数据
    orderId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      expressList: [],
      ruleForm: {
        expressName: '',
        expressPrice: '',
        expressNum: ''
      },
      rule: {
        expressNum: [
          { required: true, message: '请输入物流编号', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.onGetExpress(' ')
  },
  methods: {
    // 获取所有物流
    onGetExpress(query) {
      this.expressList = []
      getByType({
        type: '8',
        name: query
      }).then(res => {
        res.data.forEach(item => {
          let params = {}
          params.value = item.propertyName
          params.label = item.propertyValue
          this.expressList.push(params)
        })
      })
    },
    // 提交
    onSubmitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let form = new FormData()
          form.append('orderId', this.orderId)
          form.append('expressName', this.ruleForm.expressName)
          form.append('expressPrice', this.ruleForm.expressPrice)
          form.append('expressNum', this.ruleForm.expressNum)
          getOrderDeliver(form).then(res => {
            this.$notify.success({
              title: '提示',
              message: '发货成功'
            })
            this.onRefresh()
          })
        }
      })
    },
    // 取消
    onCancelForm(formName) {
      this.$refs[formName].resetFields()
      let data = { isRefresh: false }
      this.$emit('onCancelBtn', data)
    },
    // 跳转课程页
    onRefresh() {
      setTimeout(() => {
        let data = { isRefresh: true }
        this.$emit('onCancelBtn', data)
      }, 1000)
    }
  }
}
</script>

<style lang="scss" scoped>
.order-deliver {
  .el-select {
    width: 100%;
  }
}
</style>